package com.vitabuybuy.tv.ui.mine.wallet

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.widget.TextView
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.bigkoo.pickerview.view.TimePickerView
import com.blankj.utilcode.util.TimeUtils
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityOrderCommissionBinding
import com.library.common.base.BaseActivity
import com.library.common.ext.setOnclickNoRepeat
import com.vitabuybuy.tv.ui.viewmodel.NoViewModel
import kotlinx.android.synthetic.main.activity_choose_time.*
import java.text.SimpleDateFormat
import java.util.*

class ChooseTimeActivity : BaseActivity<NoViewModel, ActivityOrderCommissionBinding>() {
    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_choose_time

    private lateinit var timePickerView: TimePickerView


    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.choose_time))
        mTvRight.apply {
            text = getString(R.string.finish)
            visibility = View.VISIBLE
            setOnClickListener {
                val intent = Intent()
                val bundle = Bundle()
                bundle.putString("startTime",tvStartTime.text.toString())
                bundle.putString("endTime",tvStartTime.text.toString())
                intent.putExtras(bundle)
                finishResult(intent)
            }
        }
        initPicker()
    }

    private fun initPicker() {
        timePickerView = TimePickerBuilder(this) { date, v -> //选中事件回调
            (v as TextView).text =
                TimeUtils.date2String(date, SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()))
        }
            .setType(booleanArrayOf(true, true, true, false, false, false)) // 默认全部显示
            .setCancelText(getString(R.string.cancel)) // 取消按钮文字
            .setSubmitText(getString(R.string.confirm)) // 确认按钮文字
            .setContentTextSize(16) // 滚轮文字大小
            .setTitleSize(16) // 标题文字大小
            .setTitleText(getString(R.string.choose_time)) // 标题文字
            .setOutSideCancelable(true) // 点击屏幕，点在控件外部范围时，是否取消显示
            .isCyclic(true) // 是否循环滚动
            .setTitleColor(Color.parseColor("#323233")) // 标题文字颜色
            .setTitleBgColor(Color.WHITE) // 标题背景颜色
            .setBgColor(Color.WHITE) // 滚轮背景颜色
            .setDividerColor(Color.parseColor("#DCDDE0"))
            .setOutSideColor(Color.TRANSPARENT)
            .setSubCalSize(14)
            .setSubmitColor(Color.parseColor("#E23225"))
            .setCancelColor(Color.parseColor("#E23225"))
            .setItemVisibleCount(7)
            .build()
    }

    override fun eventHandling() {
        setOnclickNoRepeat(tvStartTime, tvEndTime){v->
            when (v) {
                tvStartTime -> {
                    timePickerView.setDate(Calendar.getInstance())
                    timePickerView.show(tvStartTime)
                }
                tvEndTime -> {
                    timePickerView.setDate(Calendar.getInstance())
                    timePickerView.show(tvEndTime)
                }
            }
        }
    }


    companion object {
        fun openActivity(mContext: BaseActivity<*, *>, requestCode: Int) {
            mContext.startForResult(null, requestCode, ChooseTimeActivity::class.java)
        }
    }
}
